package com.algrithom.unionfind;

import com.common.model.UnionFind;

/**
 * 323. 无向图中连通分量的数目
 * 
 * @author think
 */
public class Solution4 {
    
    public static void main(String[] args){
        Solution4 solution4 = new Solution4();
        int[][] edges = new int[][]{{0,1},{1,2},{3,4}};
        System.out.println(solution4.countComponents(5,edges));
    }
    
    public int countComponents(int n,int[][] edges){
        UnionFind unionFind = new UnionFind(n);
        for (int[] edge : edges) {
            if (!unionFind.isConnect(edge[0],edge[1])) {
                unionFind.union(edge[0],edge[1]);
            }
        }
        return unionFind.getCount();
    }
}